/**
 * 
 */
package org.t2.game.server.module.data.service.parser;

import java.util.List;

import org.gaming.tool.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t2.game.server.module.bag.model.Bag;
import org.t2.game.server.module.bag.service.BagService;
import org.t2.game.server.module.data.model.Data;
import org.t2.game.server.module.data.saver.PlayerWeaponNumSaver;
import org.t2.game.server.module.data.service.DataParserService;
import org.t2.game.server.module.data.struct.EquipData;

/**
 * 
 */
@Component
public class EquipParserService extends DataParserService {

	@Autowired
	private BagService bagService;
	@Autowired
	private PlayerWeaponNumSaver playerWeaponNumSaver;
	
	@Override
	public String dataType() {
		return "equip";
	}

	@Override
	public void parser(long playerId, List<Data> datas, String reason) {
		Bag bag = bagService.getEntity(playerId);
		boolean needUpdate = false;
		for(Data data : datas) {
			EquipData equipData = GsonUtil.parseJson(data.getPlaintext(), EquipData.class);
			if(equipData.getEquipData() != null && equipData.getEquipData().size() != bag.getWeaponNum()) {
				playerWeaponNumSaver.setNumber(bag, reason, equipData.getEquipData().size());
				needUpdate = true;
			}
		}
		if(needUpdate) {
			bagService.update(bag);
		}
	}

}
